//
//  HasAuthenVC.m
//  ZZLC
//
//  Created by 杨春至 on 2017/12/15.
//  Copyright © 2017年 ZhongZeng. All rights reserved.
//

#import "HasAuthenVC.h"
#import "BankModel.h"
#import "CountModel.h"

@interface CertifBGView: UIImageView

@property (nonatomic,strong) UILabel *titleLb;

@property (nonatomic,strong) UILabel *desLb;

@end

@implementation CertifBGView
- (instancetype)initWithTitle:(NSString *)titleStr andDesStr:(NSString *)desStr{
    if ([super init]) {
        self.clipsToBounds = YES;
        self.layer.cornerRadius = 4;
        [self addSubview:self.titleLb];
        [self addSubview:self.desLb];
        _titleLb.text = titleStr;
        _desLb.text = desStr;
    }
    return self;
}
- (void)layoutSubviews{
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(-2);
        make.top.equalTo(@8);
        make.width.equalTo(@(16*7));
        make.height.equalTo(@24);
    }];
    [_desLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.equalTo(self);
        make.height.equalTo(@60);
    }];
}
- (UILabel *)titleLb{
    if (!_titleLb) {
        _titleLb = [UILabel new];
        _titleLb.clipsToBounds = YES;
        _titleLb.layer.cornerRadius = 4;
        _titleLb.text = @"已实名认证";
        _titleLb.textAlignment = NSTextAlignmentCenter;
        _titleLb.backgroundColor = [UIColor whiteColor];
        _titleLb.font = [UIFont systemFontOfSize:15];
    }
    return _titleLb;
}
- (UILabel *)desLb{
    if (!_desLb) {
        _desLb = [UILabel new];
        _desLb.textAlignment = NSTextAlignmentCenter;
        _desLb.backgroundColor = [UIColor clearColor];
        _desLb.numberOfLines = 0;
        _desLb.textColor = [UIColor whiteColor];
        _desLb.text = @"中增理财\n XXXXXXX  XXXXXX XXXXXXX";
        _desLb.font = [UIFont systemFontOfSize:16];
    }
    return _desLb;
}
- (void)loadDtaBydetailStr:(NSString *)detailStr {
    _desLb.text = detailStr;
    
}
@end

@interface HasAuthenVC ()

@property (nonatomic,strong)CertifBGView *idBgView;

@property (nonatomic,strong)CertifBGView *bankBgView;

@property (nonatomic,strong)UILabel *desLb;

@property (nonatomic,strong)UIButton *resetBtn;

@property (nonatomic,strong)BankModel *bankModel;

@property (nonatomic,strong)CountModel *countModel;

@end

@implementation HasAuthenVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    self.view.backgroundColor = RGBCOLOR(245, 245, 245);
    [self.view addSubview:self.idBgView];
    [self.view addSubview:self.bankBgView];
    [self.view addSubview:self.desLb];
    [self.view addSubview:self.resetBtn];
}
- (void)dissovebankCard{
    NSString *messageStr =@"您在进行解绑操作,是否继续解绑?" ;
    BOOL canRelease = NO;
    if (_countModel) {
        messageStr = _countModel.quicklyUnbind;
        if ([_countModel.investorCount integerValue]==0&&[_countModel.totalMoney boolValue]==0) {
            messageStr = @"为保障账户信息及资金安全，解绑或者更换银行卡请联系客服人工处理";
            canRelease = YES;
        }
    }
  
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:messageStr preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *rechargeAction = [UIAlertAction actionWithTitle:canRelease?@"联系客服":@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        if (canRelease) {
            [self setupPhone:@"0571-86076015"];
        }
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertVC addAction:rechargeAction];
    [alertVC addAction:cancelAction];
    [self presentViewController:alertVC animated:YES completion:nil];
    
}
- (void)dissoveRequest{
    
    
    
}
- (void)loadData{
    if (![UserModel isLogin]) {
        [self jumpToLogVC];
        return;
    }
    NSDictionary *param = @{@"authorization":[UserModel authorization]};
    [NetWorkingManager postWithRequestUrl:@"User/showMyAccount" AndParameters:@{@"parameters":[param DataTOjsonStringAndAddVersion]} AndRequestBlock:^(NSDictionary *responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject.allKeys containsObject:@"list"]) {
            NSArray *bankArray = [BankModel objectArrayWithKeyValuesArray:responseObject[@"list"]];
            if (bankArray.count>=1) {
                self.bankModel = [bankArray firstObject];
                   [self configUI];
            }
        }
        if ([responseObject.allKeys containsObject:@"obj"]) {
            _countModel = [CountModel objectWithKeyValues:responseObject[@"obj"]];
        }
    } FailBlock:^(NSDictionary *errDic) {
        if([[errDic objectForKey:@"end"]isEqualToString:@"end"]){
            [self jumpToLogVC];
        }
    }];
    
}
- (void)configUI{
      NSString *userInfoStr = [NSString stringWithFormat:@"%@\n\n%@",_bankModel.name,_bankModel.idcard];
      NSString *bankInfoStr = [NSString stringWithFormat:@"%@\n\n%@",_bankModel.bankName,_bankModel.bankAccount];
      [_idBgView loadDtaBydetailStr:userInfoStr];
      [_bankBgView loadDtaBydetailStr:bankInfoStr];
}
- (void)viewDidLayoutSubviews{
    CGFloat gap = 15;
    [_idBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(KWidth - 15*2, (KWidth - 15*2)*0.5));
        make.centerX.equalTo(self.view);
        make.top.equalTo(@(gap));
    }];
    [_bankBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(KWidth - 15*2, (KWidth - 15*2)*0.5));
        make.centerX.equalTo(self.view);
        make.top.equalTo(_idBgView.mas_bottom).offset(gap);
    }];
    [_desLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(_bankBgView.mas_bottom).offset(gap);
        make.width.equalTo(self.view).offset(-2*gap);
        make.height.equalTo(@30);
    }];
    [_resetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-gap*2);
        make.width.equalTo(self.view).offset(-2*gap);
        make.height.equalTo(@((KWidth-2*gap)/598*96));
    }];
}
- (CertifBGView *)idBgView{
    if (!_idBgView) {
          _idBgView = [[CertifBGView alloc]initWithTitle:@"已实名认证" andDesStr:@"中增理财\n XXXXXXX  XXXXXX XXXXXXX"];
//        _idBgView.backgroundColor = [UIColor redColor];
        _idBgView.image = UIImageMake(@"idcardBg");
    }
    return _idBgView;
}
- (CertifBGView *)bankBgView{
    if (!_bankBgView) {
        _bankBgView = [[CertifBGView alloc]initWithTitle:@"已绑定银行卡" andDesStr:@"中信银行\n XXXXXXX  XXXXXX XXXXXXX"];
//        _bankBgView.backgroundColor = [UIColor greenColor];
        _bankBgView.image = UIImageMake(@"bankbg");
    }
    return _bankBgView;
}
- (UILabel *)desLb{
    if (!_desLb) {
        _desLb = [UILabel new];
        _desLb.text  = @"如您需要解绑银行卡,请先确认没有在投资项目,且资产为零.。如有疑问请联系客服。";
        _desLb.numberOfLines = 0;
        _desLb.textColor = RGBCOLOR(156, 156, 156);
        _desLb.font = [UIFont systemFontOfSize:12];
    }
    return _desLb;
}
- (UIButton *)resetBtn{
    if (!_resetBtn) {
        _resetBtn = [UIButton new];
        [_resetBtn setBackgroundImage:UIImageMake(@"btn_yellowBackground") forState:UIControlStateNormal];
        [_resetBtn setTitle:@"一键解绑" forState:UIControlStateNormal];
        [_resetBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _resetBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_resetBtn bk_addEventHandler:^(id sender) {
            [self dissovebankCard];
        } forControlEvents:UIControlEventTouchUpInside];
    }
    return _resetBtn;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
}


@end
